package com.qire.manhua.controller;

import android.view.View;

import com.google.android.material.appbar.AppBarLayout;
import com.qire.common.basic.ObjectUtil;
import com.qire.common.basic.StringUtil;
import com.qire.common.support.base.EventController;
import com.qire.common.widget.adapter.auto.AutoVariable;
import com.qire.manhua.BR;
import com.qire.manhua.activity.AccountDetailBeanActivity;
import com.qire.manhua.databinding.ActivityAccountDetailBeanBinding;
import com.qire.manhua.dialog.ObtainWaySelectorOfBeanDialog;
import com.qire.manhua.model.viewModel.impl.AccountDetailBeanViewModel;
import com.scwang.smart.refresh.layout.api.RefreshLayout;

import androidx.annotation.NonNull;
import androidx.databinding.ViewDataBinding;

public class AccountDetailBeanController extends EventController<AccountDetailBeanActivity> implements AutoVariable {

    private AccountDetailBeanViewModel viewModel;
    private ActivityAccountDetailBeanBinding viewDataBinder;

    public AccountDetailBeanController(@NonNull AccountDetailBeanActivity lifecycleOwner) {
        super(lifecycleOwner);
    }

    @Override
    protected void bindView() {
        viewDataBinder = owner.viewDataBinder();
        viewDataBinder.setAccountDetailBeanController(this);
    }

    @Override
    public void bindVariable(ViewDataBinding viewBind) {
        viewBind.setVariable(BR.accountDetailBeanController, this);
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        this.viewModel = null;
        this.viewDataBinder = null;
    }

    public void onAddition(AccountDetailBeanViewModel viewModel) {
        this.viewModel = viewModel;
    }

    /*************************************************************************************************************************/

    public void appBarLayoutOnOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        if(verticalOffset == 0) {
            //修改状态标记为展开
            viewDataBinder.balancePanel.setAlpha(1f);
        } else if(Math.abs(verticalOffset) >= appBarLayout.getTotalScrollRange()) {
            //修改状态标记为折叠
            viewDataBinder.balancePanel.setAlpha(0f);
        } else {
            //修改状态标记为中间
            int scrollRange = appBarLayout.getTotalScrollRange();
            float alpha = 1f - ((float) Math.abs(verticalOffset) / (float)scrollRange);
            viewDataBinder.balancePanel.setAlpha(alpha);
        }
    }

    public void refreshLayoutOnRefresh(RefreshLayout refreshLayout) {
        viewModel.loadData();
    }
    public void refreshLayoutOnLoadMore(RefreshLayout refreshLayout){
        viewModel.loadMoreData();
    }

    public void detailTitleOnClick(View view) {
        if(ObjectUtil.nonNull(viewDataBinder)) {
            int position = StringUtil.valueToInt(view.getTag(), 0);
            viewDataBinder.detailPager.setCurrentItem(position);
        }
    }

    public void detailPagerOnPageSelected(int position) {
        if(ObjectUtil.nonNull(viewModel)) {
            viewModel.changeDetailPagerPosition(position);
        }
    }

    public void backOnClick(View view) {
        owner.onBackPressed();
    }

    public void wayToGetBeanOnClick(View view) {
        new ObtainWaySelectorOfBeanDialog(view.getContext()).show();
    }

}
